package com.szh.zx.javacorecorret.chap2.one;

import java.util.concurrent.CountDownLatch;
// 章节2.1.1
// 实现一个当countDownLatch 的计数不为0时，线程呈现阻塞状态的效果。类似于等待一个“信号”，当信号到了，就可以走了
public class CountDownLatchTest {
	public static void main(String[] args) throws InterruptedException {
		MyService myService = new MyService();
		MyThread myThread = new MyThread(myService);
		myThread.start();
		Thread.sleep(2000);
		myService.downMethod();
	}
}
class MyService{
	private CountDownLatch downLatch = new CountDownLatch(1);
	public void testMethod(){
		try {
			System.out.println("A");
			downLatch.await();
			System.out.println("B");

		}catch (InterruptedException e)
		{
			e.printStackTrace();
		}

	}
	public void downMethod(){
		System.out.println("X");
		downLatch.countDown();
	}
}

class MyThread extends Thread{
	private MyService myService;

	public MyThread(MyService myService) {
		super();
		this.myService = myService;
	}

	@Override
	public void run() {
		myService.testMethod();
	}
}
